package com.wushi.redis;

import com.alibaba.fastjson.JSON;
import javafx.scene.chart.ValueAxis;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;

/**
 * @program:
 * @description:
 * @author: Grant Wu
 * @create: 2019-10-25 00:31
 **/
@Slf4j
public class RedisUtil {
    private static JedisPool jedisPool;


    /**
     * 使用前需要初始化jedis Pool
     *
     * @param redisHost
     */
    public static void init(String redisHost) {
        if (jedisPool == null) {
            getJedisPool(redisHost);
        }
    }

    /**
     * @param redisHost
     * @return
     */
    public static JedisPool getJedisPool(String redisHost) {
        if (StringUtils.isEmpty(redisHost)) {
            log.error("======>redisHost could not be null");
            return null;
        }
        if (jedisPool == null) {
            synchronized (RedisUtil.class) {
                if (jedisPool == null) {
                    jedisPool = new JedisPool(new GenericObjectPoolConfig(), redisHost);
                }
            }
        }
        return jedisPool;
    }

    /**
     * jedis用完后要记得close
     *
     * @param redisHost
     * @return
     */
    public static Jedis getRedis(String redisHost) {
        Jedis jedis = getJedisPool(redisHost).getResource();
        if (jedis == null) {
            log.error("======>jedis client is null");
        }
        return jedis;
    }

    /**
     * jedis用完后要记得close
     *
     * @param
     * @return
     */
    public static Jedis getRedis() {
        if (jedisPool == null) {
            log.error("======>jedis client is null");
        }
        Jedis jedis = jedisPool.getResource();
        return jedis;
    }

    public static String stringSet(String key, String value,int timeOutSeconds) {
        Jedis redis = getRedis();
        try {
            String set = redis.set(key, value);
            redis.expire(key,timeOutSeconds);
            return set;
        } catch (Exception e) {
            log.error("======> redis set error:", e);
        } finally {
            redis.close();
        }
        return null;
    }

    public static String stringGet(String key) {
        Jedis redis = getRedis();
        try {
            String s = redis.get(key);
            return s;
        } catch (Exception e) {
            log.error("======> redis stringGet error:", e);
        } finally {
            redis.close();
        }
        return null;
    }

    /**
     * @param key-map  key
     * @param hashkey: map内具体的key
     * @return
     */
    public static String hashGet(String key, String hashkey) {
        Jedis redis = getRedis();
        try {
            String value = redis.hget(key, hashkey);
            return value;
        } catch (Exception e) {
            log.error("======> redis hashGet error:", e);
        } finally {
            redis.close();
        }
        return null;
    }

    /**
     * @param key-map  key
     * @param hashkey: map内具体的key
     * @return
     */
    public static Long hashSet(String key, String hashkey, String value, Integer timeoutSeconds) {
        Jedis redis = getRedis();
        try {
            Long hset = redis.hset(key, hashkey, value);
            if(timeoutSeconds!=null) {
                redis.expire(key, timeoutSeconds);
            }
            return hset;
        } catch (Exception e) {
            log.error("======> redis hashGet error:", e);
        } finally {
            redis.close();
        }
        return null;
    }

    /**
     * @param key-map  key
     * @param hashkey: map内具体的key
     * @return
     */
    public static Long hashSetValues(String key, String hashkey, List<String> values, Integer timeoutSeconds) {
        Jedis redis = getRedis();
        try {
            Long hset = redis.hset(key, hashkey, JSON.toJSONString(values));
            if(timeoutSeconds!=null) {
                redis.expire(key, timeoutSeconds);
            }
            return hset;
        } catch (Exception e) {
            log.error("======> redis hashGet error:", e);
        } finally {
            redis.close();
        }
        return null;
    }

    /**
     * @param key-map  key
     * @param hashkey: map内具体的key
     * @return
     */
    public static List<String> hashGetValues(String key, String hashkey) {
        Jedis redis = getRedis();
        try {
            String value = redis.hget(key, hashkey);
            if(StringUtils.isEmpty(value)){
                return null;
            }
            return JSON.parseArray(value,String.class);
        } catch (Exception e) {
            log.error("======> redis hashGet error:", e);
        } finally {
            redis.close();
        }
        return null;
    }

    /**
     * 判断hash内是否包含某个key
     * @param key-map  key
     * @param hashkey: map内具体的key
     * @return
     */
    public static boolean hashSetContains(String key, String hashkey) {
        Jedis redis = getRedis();
        try {
            Boolean hexists = redis.hexists(key, hashkey);
            return hexists;
        } catch (Exception e) {
            log.error("======> redis hashGet error:", e);
        } finally {
            redis.close();
        }
        return false;
    }

    /**
     * @param
     * @param key -map key
     * @return
     */
    public static Map<String, String> hashGetAll(String key) {
        Jedis redis = getRedis();
        try {
            Map<String, String> map = redis.hgetAll(key);
            return map;
        } catch (Exception e) {
            log.error("======> redis hashGetAll error:", e);
        } finally {
            redis.close();
        }
        return null;
    }

    public static Long getLock(String key, String value, int timeoutSeconds) {
        Jedis redis = getRedis();
        try {
            Long setnx = redis.setnx(key, value);
            Long expire = redis.expire(key, timeoutSeconds);
            return expire;
        } catch (Exception e) {
            log.error("======> redis getLock error:", e);
        } finally {
            redis.close();
        }
        return null;
    }

    public static boolean releaseLock(String key,String value) {
        Jedis redis = getRedis();
        try {
            String cacheValue = redis.get(key);
            if(StringUtils.isNotEmpty(cacheValue) && cacheValue.equals(value)) {
                Long del = redis.del(key);
                return true;
            }else{
                log.warn("======>the value is null or not equal wich lock value:{}-{}",value,cacheValue);
                return false;
            }
        } catch (Exception e) {
            log.error("======> redis getLock error:", e);
        } finally {
            redis.close();
        }
        return false;
    }
}